package cn.com.guage.single.idler;

/**
 * 双重检查方式
 * @author yangdechao
 *
 */
public class Singleton4 {

	//使用volatile修饰，禁止重排序
	private static volatile  Singleton4 instance;

	public Singleton4() {
	}

	public static Singleton4 getInstance() {
		if (instance == null) {
			synchronized (Singleton4.class) {
				if (instance == null) {
					instance = new Singleton4();
				}

			}

		}
		return instance;
	}

	public void say() {
		System.out.println("say");
	}

	public static void main(String[] args) {
		Singleton4 instance1 = Singleton4.getInstance();
		System.out.println(instance1);
		instance1.say();
		Singleton4 instance2 = Singleton4.getInstance();
		System.out.println(instance2);
	}

}
